package cn.zch.soft.util.qrcode.utils;
public class Convert {
  /**********************将两个ASCII字符合成一个字节； 如："EF"--> 0xEF**************************************/
  public static byte uniteBytes(byte src0, byte src1) {
    byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue();
    _b0 = (byte) (_b0 << 4);
    byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue();
    byte ret = (byte) (_b0 ^ _b1);
    return ret;
  }
  /*******************将指定字符串src，以每两个字符分割转换为16进制形式 如："2B44EFD9" –> byte[]{0x2B, 0×44, 0xEF,*************/
  public static byte[] HexString2Bytes(String src) {
    byte[] ret = new byte[src.length() / 2];
    byte[] tmp = src.getBytes();
    for (int i = 0; i < tmp.length / 2; i++) {
      ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
    }
    return ret;
  }
  /**********************将指定byte数组以16进制的形式打印到控制台****************************************************/
  public static void printHexString(String hint, byte[] b) {
    System.out.print(hint);
    for (int i = 0; i < b.length; i++) {
      String hex = Integer.toHexString(b[i] & 0xFF);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }
      System.out.print(hex.toUpperCase() + " ");
    }
    System.out.println();
  }
  /*********************************************************将字节数组转换为16进制字符串*****************************/
  public static String Bytes2HexString(byte[] b) {
    String ret = "";
    for (int i = 0; i < b.length; i++) {
      String hex = Integer.toHexString(b[i] & 0xFF);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }
      ret += hex.toUpperCase();
    }
    return ret;
  }
  /**********************符串字符串到十六进制字********************************************************************/
  public static String string2HexString(String strPart) {
    StringBuffer hexString = new StringBuffer();
    for (int i = 0; i < strPart.length(); i++) {
      int ch = strPart.charAt(i);
      String strHex = Integer.toHexString(ch);
      hexString.append(strHex);
    }
    return hexString.toString();
  }
  /**********************十六进制字符串到字符串********************************************************************/
  public static String hexString2String(String src) {
    String temp = "";
    for (int i = 0; i < src.length() / 2; i++) {
      temp = temp + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
    }
    return temp;
  }
  /*************************************十六进制字符串到int*****************************************************/
  public static int HexStringtoInt(String ox) throws Exception {
    ox = ox.toLowerCase();
    if (ox.startsWith("0x")) {
      ox = ox.substring(2);
    }
    int ri = 0;
    int oxlen = ox.length();
    if (oxlen > 8)
      throw (new Exception("too lang"));
    for (int i = 0; i < oxlen; i++) {
      char c = ox.charAt(i);
      int h;
      if (('0' <= c && c <= '9')) {
        h = c - 48;
      }
      else if (('a' <= c && c <= 'f')) {
        h = c - 87;
      }
      else if ('A' <= c && c <= 'F') {
        h = c - 55;
      }
      else {
        throw (new Exception("not a integer "));
      }
      byte left = (byte) ((oxlen - i - 1) * 4);
      ri |= (h << left);
    }
    return ri;
  }
  /*************************************int到十六进制字符串*****************************************************/
  public static String intToHexString(int a, int len) {
    len <<= 1;
    String hexString = Integer.toHexString(a);
    int b = len - hexString.length();
    if (b > 0) {
      for (int i = 0; i < b; i++) {
        hexString = "0" + hexString;
      }
    }
    return hexString;
  }
  /******************************CRC16****************************************************************************/
  public static String CRC_CCITT(int flag, byte[] bytes) {
    int crc = 0x00; // initial value
    int polynomial = 0x1021;
    switch (flag) {
      case 1:
        crc = 0x00;
        break;
      case 2:
        crc = 0xFFFF;
        break;
      case 3:
        crc = 0x1D0F;
        break;
    }
    for (int index = 0; index < bytes.length; index++) {
      byte b = bytes[index];
      for (int i = 0; i < 8; i++) {
        boolean bit = ((b >> (7 - i) & 1) == 1);
        boolean c15 = ((crc >> 15 & 1) == 1);
        crc <<= 1;
        if (c15 ^ bit)
          crc ^= polynomial;
      }
    }
    crc &= 0xffff;
    String data = Integer.toHexString(crc);
    return data;
  }
}
